import 'package:flutter/material.dart';
import 'package:love_app/enum/comment_type.dart';
import 'package:love_app/model/comment_detailst_model.dart' as CDModel;

// 文章详情共享数据
class DeatilsDataProvider with ChangeNotifier{
  
  // 文章id
  int _itemId;

  // 评论数据
  List<CDModel.Data> _deatilsData = [];

  // 滚动条文职
  double _offset = 0.0;

  // 是否是最后一页
  bool _isEndPage = false;

  // 当前页面
  int _page = 1;

  // 是否是第一页
  bool _isFirst = true;

  // 有多少条评论
  int _index = 0;


  int get index => this._index;
  set index(int index){
    this._index = index;
  }

  bool get isFirst => this._isFirst;
  set isFirst(bool isFirst){
    this._isFirst = isFirst;
  }
  
  int get itemId => this._itemId;
  set itemId(int itemID){
    this._itemId = itemID;
  }

  List<CDModel.Data> get deatilsData => this._deatilsData;
  set deatilsData(List<CDModel.Data> deatilsData){
    this._deatilsData = deatilsData;
  }

  double get offset => this._offset;
  set offset(double offset){
    this._offset = offset;
  }

  bool get isEndPage => this._isEndPage;
  set isEndPage(bool isEndPage){
    this._isEndPage = isEndPage;
  }

  int get page => this._page;
  set page(int _page){
    this._page = _page;
  }

  // 根据id判断当前的值是否需要重新发起请求
  bool hasData(int itemId){
    if( itemId == this._itemId && this.deatilsData.length > 0){
      return true;
    }
    clean();
    return false;
  }
  void clean(){
    this._deatilsData.clear();
    this._offset = 0.0;
    this._page = 1;
    this._isEndPage = false;
  }

  void deleteByCId(int cId, {CommentType type = CommentType.first}){
    if(type == CommentType.first){
      this._deatilsData.removeWhere((item)=>item.cId == cId);
    }else{

    }
  }

  // 往list添加数据
  void addDeatilsData(CDModel.Data item, {bool first = false}){
    if(first == false){
      this._deatilsData.add(item);
    }else{
      this._deatilsData.insert(0, item);
    }
  }

  // 通知更新
  void notify(){
    notifyListeners();
  }
}